(IFNULL(SUM(d.count),0) - (SELECT IFNULL(SUM(de.count),0) FROM as_busrepair_inventorybill_detail as de LEFT JOIN as_busrepair_inventorybill as be ON de.billSid = be.sid WHERE de.inventorySid = d.inventorySid AND be.sourceBillSid = a.sid AND b.busTypeKey = '1') ) as num,
(IFNULL(SUM(d.amount),0) - (SELECT IFNULL(SUM(de.amount),0) FROM as_busrepair_inventorybill_detail as de LEFT JOIN as_busrepair_inventorybill as be ON de.billSid = be.sid WHERE de.inventorySid = d.inventorySid AND be.sourceBillSid = a.sid AND b.busTypeKey = '1') ) as discountAmount,
(IFNULL(SUM(d.costTotal),0) - (SELECT IFNULL(SUM(de.costTotal),0) FROM as_busrepair_inventorybill_detail as de LEFT JOIN as_busrepair_inventorybill as be ON de.billSid = be.sid WHERE de.inventorySid = d.inventorySid AND be.sourceBillSid = a.sid AND b.busTypeKey = '1') ) as costTotal,
((IFNULL(SUM(d.amount),0) - (SELECT IFNULL(SUM(de.amount),0) FROM as_busrepair_inventorybill_detail as de LEFT JOIN as_busrepair_inventorybill as be ON de.billSid = be.sid WHERE de.inventorySid = d.inventorySid AND be.sourceBillSid = a.sid AND b.busTypeKey = '1') ) - (IFNULL(SUM(d.costTotal),0) - (SELECT IFNULL(SUM(de.costTotal),0) FROM as_busrepair_inventorybill_detail as de LEFT JOIN as_busrepair_inventorybill as be ON de.billSid = be.sid WHERE de.inventorySid = d.inventorySid AND be.sourceBillSid = a.sid AND b.busTypeKey = '1') ) ) as profit
FROM
as_busrepair_inventorybill_detail AS d
LEFT JOIN as_busrepair_inventorybill AS b ON d.billSid = b.sid
LEFT JOIN as_busrepair_bill AS a ON b.sourceBillSid = a.sid
LEFT JOIN yxt_wms.wms_inventory AS i ON d.inventorySid = i.sid
(IFNULL(SUM(d.count),0) - (SELECT IFNULL(SUM(de.count),0) FROM as_busrepair_inventorybill_detail as de LEFT JOIN as_busrepair_inventorybill as be ON de.billSid = be.sid WHERE de.inventorySid = d.inventorySid AND be.sourceBillSid = a.sid AND b.busTypeKey = '1') ) as num,
d.discount,
(IFNULL(SUM(d.amount),0) - (SELECT IFNULL(SUM(de.amount),0) FROM as_busrepair_inventorybill_detail as de LEFT JOIN as_busrepair_inventorybill as be ON de.billSid = be.sid WHERE de.inventorySid = d.inventorySid AND be.sourceBillSid = a.sid AND b.busTypeKey = '1') ) as discountAmount,
d.cost,
(IFNULL(SUM(d.costTotal),0) - (SELECT IFNULL(SUM(de.costTotal),0) FROM as_busrepair_inventorybill_detail as de LEFT JOIN as_busrepair_inventorybill as be ON de.billSid = be.sid WHERE de.inventorySid = d.inventorySid AND be.sourceBillSid = a.sid AND b.busTypeKey = '1') ) as costTotal,
((IFNULL(SUM(d.amount),0) - (SELECT IFNULL(SUM(de.amount),0) FROM as_busrepair_inventorybill_detail as de LEFT JOIN as_busrepair_inventorybill as be ON de.billSid = be.sid WHERE de.inventorySid = d.inventorySid AND be.sourceBillSid = a.sid AND b.busTypeKey = '1') ) - (IFNULL(SUM(d.costTotal),0) - (SELECT IFNULL(SUM(de.costTotal),0) FROM as_busrepair_inventorybill_detail as de LEFT JOIN as_busrepair_inventorybill as be ON de.billSid = be.sid WHERE de.inventorySid = d.inventorySid AND be.sourceBillSid = a.sid AND b.busTypeKey = '1') ) ) as profit,
(SELECT GROUP_CONCAT( r.repairerName SEPARATOR ',' ) FROM as_busrepair_bill_sitem_repairer as r WHERE r.billSid = a.sid and r.repairitemSid = d.serviceItemSid) as repairerName,
a.customerName,
v.vinNo,
v.vehMark,
v.vehModel
FROM as_busrepair_bill_sitem as d
LEFT JOIN as_busrepair_bill as a ON d.billSid = a.sid
LEFT JOIN as_busrepair_bill_vech as v ON v.billSid = a.sid
UNION ALL
SELECT
a.orgSidPath,
a.`deptName`,
a.`useOrgName`,
d.billBusTypeValue busType,
d.attachItem `name`,
a.nodeName billState,
'' serviceType,
a.billType,
a.`subject`,
'' goodsSkuCode,
'' supplierName,
'' manufacturerName,
IFNULL(d.price,0) price,
'1' num,
'' discount,
IFNULL(d.price,0) as discountAmount,
IFNULL(d.cost,0)as cost,
IFNULL(d.costTotal,0)as costTotal,
((IFNULL(d.price,0)) - (IFNULL(d.costTotal,0))) as profit,
DATEDIFF(NOW(),(SELECT MIN(wi.firstInDate) FROM wms_inventory as wi WHERE wi.count > 0 and wi.useOrgSid = i.useOrgSid and wi.goodsSkuCode = i.goodsSkuCode and wi.manufacturerSid = i.manufacturerSid and wi.supplierSid = i.supplierSid)) inventoryAge,
date_format ((SELECT MAX(r.createTime) FROM wms_inventory_record as r LEFT JOIN wms_inventory as wi on r.inventorySid = wi.sid WHERE wi.useOrgSid = i.useOrgSid and wi.goodsSkuCode = i.goodsSkuCode and wi.manufacturerSid = i.manufacturerSid and wi.supplierSid = i.supplierSid and r.busTypeValue = '销售出库'),'%Y-%m-%d') salesTime,
DATEDIFF(NOW(),(date_format ((SELECT MAX(r.createTime) FROM wms_inventory_record as r LEFT JOIN wms_inventory as wi on r.inventorySid = wi.sid WHERE wi.useOrgSid = i.useOrgSid and wi.goodsSkuCode = i.goodsSkuCode and wi.manufacturerSid = i.manufacturerSid and wi.supplierSid = i.supplierSid and r.busTypeValue = '销售出库'),'%Y-%m-%d'))) noSalesDays,
date_format ((SELECT MAX(r.createTime) FROM wms_inventory_record as r LEFT JOIN wms_inventory as wi on r.inventorySid = wi.sid WHERE wi.useOrgSid = i.useOrgSid and wi.goodsSkuCode = i.goodsSkuCode and wi.manufacturerSid = i.manufacturerSid and wi.supplierSid = i.supplierSid and r.billType = '1'),'%Y-%m-%d') storageTime,