| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399 |
- package restcontroller.webmanage.sale.order;
- import beans.datacontrllog.DataContrlLog;
- import beans.itemprice.ItemPrice;
- import com.alibaba.fastjson.JSONArray;
- import com.alibaba.fastjson.JSONObject;
- import common.BaseClass;
- import common.Controller;
- import common.YosException;
- import common.data.*;
- import java.math.BigDecimal;
- import java.util.ArrayList;
- public class OrderItemsHelper extends BaseClass {
- Controller controller;
- JSONObject content;
- public OrderItemsHelper(Controller controller) {
- this.controller = controller;
- this.content = controller.content;
- }
- /**
- * 是否符合商品起订量和增量判断
- *
- * @param type
- * @param itemRowsMap
- * @param item
- * @param itemRow
- * @param promotionitems
- * @throws YosException
- */
- public void compareQty(String type, RowsMap itemRowsMap, JSONObject item, Row itemRow, Rows promotionitems) throws YosException {
- BigDecimal qty = item.getBigDecimalValue("qty");
- String itemid = item.getString("itemid");
- if (!type.equals("促销订单")) {
- if (itemRowsMap.containsKey(itemid)) {
- if (itemRowsMap.get(itemid).isNotEmpty()) {
- BigDecimal orderaddqty = itemRowsMap.get(itemid).get(0).getBigDecimal("orderaddqty");
- BigDecimal orderminqty = itemRowsMap.get(itemid).get(0).getBigDecimal("orderminqty");
- if (orderaddqty.compareTo(BigDecimal.ZERO) != 0) {
- if (((qty.subtract(orderminqty)).remainder(orderaddqty)).compareTo(BigDecimal.ZERO) != 0 || qty.compareTo(orderminqty) < 0) {
- throw new YosException(false, "品号为" + itemRowsMap.get(itemid).get(0).getString("itemno") + "的商品数量不符合该商品的起订量和增量");
- }
- }
- }
- }
- }
- RowsMap pRowsMap = promotionitems.toRowsMap("itemid");
- if (type.equals("促销订单")) {
- if (pRowsMap.containsKey(itemid)) {
- if (pRowsMap.get(itemid).isNotEmpty()) {
- BigDecimal saledqty = pRowsMap.get(itemid).get(0).getBigDecimal("saledqty");
- BigDecimal groupqty = pRowsMap.get(itemid).get(0).getBigDecimal("groupqty");
- BigDecimal orderaddqty = pRowsMap.get(itemid).get(0).getBigDecimal("orderaddqty");
- BigDecimal orderminqty = pRowsMap.get(itemid).get(0).getBigDecimal("orderminqty");
- boolean islimit = pRowsMap.get(itemid).get(0).getBoolean("islimit");
- if (orderaddqty.compareTo(BigDecimal.ZERO) != 0) {
- if (((qty.subtract(orderminqty)).remainder(orderaddqty)).compareTo(BigDecimal.ZERO) != 0 || qty.compareTo(orderminqty) < 0) {
- throw new YosException(false, "品号为" + itemRowsMap.get(itemid).get(0).getString("itemno") + "的商品数量不符合该商品的起订量和增量");
- }
- }
- if (islimit) {
- if (groupqty.compareTo(saledqty.add(qty)) < 0) {
- throw new YosException(false, "品号:" + itemRow.getString("itemno") + "已超过限购数量:" + groupqty.subtract(saledqty));
- }
- }
- }
- }
- }
- }
- /**
- * 获取定制属性
- *
- * @param itemRowsMap
- * @param item
- * @return
- * @throws YosException
- */
- public JSONArray getCustomProperties(RowsMap itemRowsMap, JSONObject item) throws YosException {
- JSONArray customproperties = new JSONArray();
- Rows customschemeRows = dbConnect.runSqlQuery("select * from sa_customscheme_items where siteid='" + controller.siteid + "'");
- RowsMap customschemeRowsMap = customschemeRows.toRowsMap("sa_customschemeid");
- String itemid = item.getString("itemid");
- if (itemRowsMap.containsKey(itemid)) {
- if (itemRowsMap.get(itemid).isNotEmpty()) {
- if (itemRowsMap.get(itemid).get(0).getLong("sa_customschemeid") > 0 && !itemRowsMap.get(itemid).get(0).getBoolean("iscustomsize")) {
- // System.out.println(0000);
- if (customschemeRowsMap.containsKey(itemRowsMap.get(itemid).get(0).getString("sa_customschemeid"))) {
- // System.out.println(111111);
- for (Row customschemeItem : (customschemeRowsMap.get(itemRowsMap.get(itemid).get(0).getString("sa_customschemeid")))) {
- // System.out.println(customschemeItem.getString("description"));
- // System.out.println(2222);
- customproperties.add(customschemeItem.getString("description") + ":" + itemRowsMap.get(itemid).get(0).getString(customschemeItem.getString("value")));
- }
- }
- } else if (itemRowsMap.get(itemid).get(0).getBoolean("iscustomsize")) {
- // System.out.println(3333);
- if (customschemeRowsMap.containsKey(itemRowsMap.get(itemid).get(0).getString("sa_customschemeid"))) {
- // System.out.println(4444);
- for (Row customschemeItem : (customschemeRowsMap.get(itemRowsMap.get(itemid).get(0).getString("sa_customschemeid")))) {
- // System.out.println(5555);
- // System.out.println((customschemeRowsMap.get(itemRowsMap.get(itemid).get(0).getString("sa_customschemeid"))).size());
- if (!customschemeItem.getString("value").equals("spec")) {
- customproperties.add(customschemeItem.getString("description") + ":" + itemRowsMap.get(itemid).get(0).getString(customschemeItem.getString("value")));
- } else {
- customproperties.add("尺寸:" + item.getStringValue("length") + "*" + item.getStringValue("width"));
- }
- }
- } else {
- customproperties.add("尺寸:" + item.getStringValue("length") + "*" + item.getStringValue("width"));
- }
- }
- }
- }
- return customproperties;
- }
- /**
- * 获取定制尺寸信息
- *
- * @param itemRowsMap
- * @param item
- * @return
- * @throws YosException
- */
- public String getCustomSpec(RowsMap itemRowsMap, JSONObject item) throws YosException {
- String spec="";
- Rows customschemeRows = dbConnect.runSqlQuery("select * from sa_customscheme_items where siteid='" + controller.siteid + "'");
- RowsMap customschemeRowsMap = customschemeRows.toRowsMap("sa_customschemeid");
- String itemid = item.getString("itemid");
- if (itemRowsMap.containsKey(itemid)) {
- if (itemRowsMap.get(itemid).isNotEmpty()) {
- if (!itemRowsMap.get(itemid).get(0).getBoolean("iscustomsize")) {
- spec=itemRowsMap.get(itemid).get(0).getString("spec");
- } else {
- spec=item.getStringValue("length") + "*" + item.getStringValue("width");
- }
- }
- }
- return spec;
- }
- /**
- * 批量查询商品信息
- *
- * @param items
- * @return
- * @throws YosException
- */
- public RowsMap getItemRowsMap(JSONArray items) throws YosException {
- ArrayList<Long> itemids = new ArrayList<>();
- for (Object obj : items) {
- JSONObject item = (JSONObject) obj;
- itemids.add(item.getLong("itemid"));
- }
- String sql = "select t1.*,t2.material from plm_item t1 left join plm_itemextend t2 ON t2.itemid = t1.itemid AND t2.siteid = t1.siteid where t1.siteid='" + controller.siteid + "' and t1.itemid in " + itemids;
- sql = sql.replace("[", "(").replace("]", ")");
- return dbConnect.runSqlQuery(sql).toRowsMap("itemid");
- }
- public UpdateSQL getUpdateSql() throws YosException {
- System.err.println("123");
- UpdateSQL updateSQL = SQLFactory.createUpdateSQL(controller, "sa_order");
- if (controller.content.containsKey("sa_order_v")) {
- updateSQL.setDataVersion(content.getIntValue("sa_order_v"));
- }
- updateSQL.setValue("changeby", controller.username);
- updateSQL.setWhere("sa_orderid", content.getLongValue("sa_orderid"));
- updateSQL.setWhere("siteid", controller.siteid);
- System.err.println(updateSQL.getSQL());
- return updateSQL;
- }
- String sa_orderitems = "sa_orderitems";
- public InsertSQL getInsertSQL(JSONArray customproperties, long sa_orderitemsid, JSONObject item, Row itemRow, String type, BigDecimal defaultprice, BigDecimal price,BigDecimal saleprice, BigDecimal marketprice,String spec) throws YosException {
- Long itemid = item.getLong("itemid");
- BigDecimal qty = item.getBigDecimalValue("qty");
- InsertSQL insertSQL = SQLFactory.createInsertSQL(controller, sa_orderitems);
- insertSQL.setValue("customproperties", customproperties.toJSONString().replace("[", "").replace("]", "").replace("\"", ""));
- insertSQL.setValue("siteid", controller.siteid);
- insertSQL.setValue("sa_orderitemsid", sa_orderitemsid);
- insertSQL.setValue("sa_orderid", content.getLongValue("sa_orderid"));
- insertSQL.setValue("rowno", 1);
- insertSQL.setValue("itemid", itemid);
- insertSQL.setValue("remarks", item.getStringValue("remarks"));
- insertSQL.setValue("itemno", itemRow.getString("itemno"));
- insertSQL.setValue("itemname", itemRow.getString("itemname"));
- insertSQL.setValue("model", itemRow.getString("model"));
- insertSQL.setValue("unit", itemRow.getString("unit"));
- insertSQL.setValue("auxunit", itemRow.getString("auxunit"));
- insertSQL.setValue("batchcontrol", itemRow.getLong("batchcontrol"));
- insertSQL.setValue("stockno", item.getOrDefault("stockno", "null"));
- insertSQL.setValue("position", item.getOrDefault("position", "null"));
- insertSQL.setValue("batchno", item.getOrDefault("batchno", "null"));
- insertSQL.setValue("spec",spec);
- String deliverydate = item.getStringValue("deliverydate");
- Rows rows = dbConnect.runSqlQuery("SELECT deliverydate,defaultprice from sa_orderitems WHERE sa_orderitemsid =" + sa_orderitemsid);
- if (deliverydate.isEmpty()) {
- if (rows.isNotEmpty()) {
- deliverydate = rows.get(0).getString("deliverydate");
- } else {
- deliverydate = "null";
- }
- }
- String needdate = item.getStringValue("needdate");
- insertSQL.setValue("needdate", needdate.equals("") ? "null" : needdate);
- insertSQL.setValue("deliverydate", deliverydate.equals("") ? "null" : deliverydate);
- BigDecimal conversionrate = itemRow.getBigDecimal("conversionrate");
- if (conversionrate.compareTo(BigDecimal.ZERO) <= 0) {
- conversionrate = BigDecimal.valueOf(1);
- }
- insertSQL.setValue("conversionrate", conversionrate);
- //订购数量
- insertSQL.setValue("qty", qty);
- insertSQL.setValue("undeliqty", qty);
- //辅助单位数量
- insertSQL.setValue("auxqty", qty.divide(conversionrate));
- if (type.equals("特殊订单")) {
- if (item.containsKey("defaultprice")) {
- if (item.getBigDecimalValue("price").compareTo(BigDecimal.ZERO) > 0 && item.getBigDecimalValue("amount").compareTo(BigDecimal.ZERO) <= 0) {
- insertSQL.setValue("price", item.getBigDecimalValue("price"));
- //折前金额(元)
- insertSQL.setValue("amount", price.multiply(qty));
- } else if (item.getBigDecimalValue("price").compareTo(BigDecimal.ZERO) <= 0 && item.getBigDecimalValue("amount").compareTo(BigDecimal.ZERO) > 0) {
- insertSQL.setValue("price", item.getBigDecimalValue("amount").divide(qty, 6, BigDecimal.ROUND_HALF_UP));
- //折前金额(元)
- insertSQL.setValue("amount", item.getBigDecimalValue("amount"));
- } else {
- insertSQL.setValue("price", price);
- //折前金额(元)
- insertSQL.setValue("amount", price.multiply(qty));
- }
- } else {
- insertSQL.setValue("price", price);
- //折前金额(元)
- insertSQL.setValue("amount", price.multiply(qty));
- }
- } else {
- insertSQL.setValue("price", price);
- //折前金额(元)
- insertSQL.setValue("amount", price.multiply(qty));
- }
- //单价,折后价(元),取合同价
- insertSQL.setValue("defaultprice", defaultprice);
- insertSQL.setValue("saleprice", saleprice);
- //金额,折后金额(元)
- insertSQL.setValue("defaultamount", defaultprice.multiply(qty));
- //牌价、市场价(元),标准订单牌价取商品价格,项目订单取合同里的牌价
- insertSQL.setValue("marketprice", marketprice);
- return insertSQL;
- }
- public UpdateSQL getUpdateSQL(long sa_orderitemsid, JSONObject item, Row itemRow, String type, BigDecimal defaultprice, BigDecimal price,BigDecimal saleprice) throws YosException {
- BigDecimal qty = item.getBigDecimalValue("qty");
- UpdateSQL updateSQL = SQLFactory.createUpdateSQL(controller, sa_orderitems);
- //订购数量
- updateSQL.setValue("qty", qty);
- //辅助单位数量
- BigDecimal conversionrate = itemRow.getBigDecimal("conversionrate");
- if (conversionrate.compareTo(BigDecimal.ZERO) <= 0) {
- conversionrate = BigDecimal.valueOf(1);
- }
- updateSQL.setValue("auxqty", qty.divide(conversionrate));
- updateSQL.setValue("undeliqty", qty.subtract(itemRow.getBigDecimal("delivery")));
- updateSQL.setValue("remarks", item.getStringValue("remarks"));
- updateSQL.setValue("stockno", item.getOrDefault("stockno", "null"));
- updateSQL.setValue("position", item.getOrDefault("position", "null"));
- updateSQL.setValue("batchno", item.getOrDefault("batchno", "null"));
- String deliverydate = item.getStringValue("deliverydate");
- Rows rows = dbConnect.runSqlQuery("SELECT deliverydate,defaultprice from sa_orderitems WHERE sa_orderitemsid =" + sa_orderitemsid);
- if (deliverydate.isEmpty()) {
- if (rows.isNotEmpty()) {
- deliverydate = rows.get(0).getString("deliverydate");
- } else {
- deliverydate = "null";
- }
- }
- String needdate = item.getStringValue("needdate");
- updateSQL.setValue("needdate", needdate.equals("") ? "null" : needdate);
- updateSQL.setValue("deliverydate", deliverydate.equals("") ? "null" : deliverydate);
- if (type.equals("特殊订单")) {
- if (item.containsKey("defaultprice")) {
- if (item.getBigDecimalValue("price").compareTo(BigDecimal.ZERO) > 0 && item.getBigDecimalValue("amount").compareTo(BigDecimal.ZERO) <= 0) {
- updateSQL.setValue("price", item.getBigDecimalValue("price"));
- //折前金额(元)
- updateSQL.setValue("amount", price.multiply(qty));
- } else if (item.getBigDecimalValue("price").compareTo(BigDecimal.ZERO) <= 0 && item.getBigDecimalValue("amount").compareTo(BigDecimal.ZERO) > 0) {
- updateSQL.setValue("price", item.getBigDecimalValue("amount").divide(qty, 6, BigDecimal.ROUND_HALF_UP));
- //折前金额(元)
- updateSQL.setValue("amount", item.getBigDecimalValue("amount"));
- } else {
- updateSQL.setValue("price", price);
- //折前金额(元)
- updateSQL.setValue("amount", price.multiply(qty));
- }
- } else {
- updateSQL.setValue("price", price);
- //折前金额(元)
- updateSQL.setValue("amount", price.multiply(qty));
- }
- } else {
- updateSQL.setValue("price", price);
- //折前金额(元)
- updateSQL.setValue("amount", price.multiply(qty));
- }
- //单价,折后价(元),取合同价
- updateSQL.setValue("defaultprice", defaultprice);
- //金额,折后金额(元)
- updateSQL.setValue("defaultamount", defaultprice.multiply(qty));
- updateSQL.setValue("saleprice", saleprice);
- updateSQL.setWhere("siteid", controller.siteid);
- updateSQL.setWhere("sa_orderitemsid", sa_orderitemsid);
- return updateSQL;
- }
- public BigDecimal getDefaultprice(ItemPrice itemPrice) throws YosException {
- return itemPrice.getGraderateprice();
- }
- public BigDecimal getPrice(Row orderRow, ItemPrice itemPrice, JSONObject item, RowsMap itemRowsMap) throws YosException {
- String type = orderRow.getString("type");
- Long sa_promotionid = orderRow.getLong("sa_promotionid");
- BigDecimal price;
- switch (type) {
- case "促销订单":
- price = itemPrice.getPromotionPrice(sa_promotionid);
- if (itemRowsMap.containsKey(item.getString("itemid"))) {
- if (itemRowsMap.get(item.getString("itemid")).isNotEmpty()) {
- if (itemRowsMap.get(item.getString("itemid")).get(0).getBoolean("iscustomsize") && itemRowsMap.get(item.getString("itemid")).get(0).getLong("pricingmetod") == 1) {
- price = price.multiply(item.getBigDecimalValue("length").multiply(item.getBigDecimalValue("width")).divide(new BigDecimal("1000000"), 4, BigDecimal.ROUND_HALF_UP));
- }
- }
- }
- break;
- default:
- price = itemPrice.getGraderateprice();
- if (itemRowsMap.containsKey(item.getString("itemid"))) {
- if (itemRowsMap.get(item.getString("itemid")).isNotEmpty()) {
- if (itemRowsMap.get(item.getString("itemid")).get(0).getBoolean("iscustomsize") && itemRowsMap.get(item.getString("itemid")).get(0).getLong("pricingmetod") == 1) {
- price = price.multiply(item.getBigDecimalValue("length").multiply(item.getBigDecimalValue("width")).divide(new BigDecimal("1000000"), 4, BigDecimal.ROUND_HALF_UP));
- }
- }
- }
- }
- return price;
- }
- public BigDecimal getSalePrice(Row orderRow, ItemPrice itemPrice) throws YosException {
- String type = orderRow.getString("type");
- Long sa_promotionid = orderRow.getLong("sa_promotionid");
- BigDecimal price;
- switch (type) {
- case "促销订单":
- price = itemPrice.getPromotionPrice(sa_promotionid);
- break;
- default:
- price = itemPrice.getGraderateprice();
- }
- return price;
- }
- }
|